﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using TileEngine;

namespace CarlosMisfortune.model
{
    public static class LevelManager
    {
        #region Fields
        //i view istället?
        private static ContentManager m_content;
        private static PlayerModel m_player;
        private static int m_currentLevel;
        private static Vector2 m_respawnPos;
        #endregion

        #region Properties
        public static int CurrentLevel
        {
            get { return m_currentLevel; }
            set { m_currentLevel = value; }
        }

        public static Vector2 RespawnPos
        {
            get { return m_respawnPos; }
            set { m_respawnPos = value; }
        }
        #endregion

        #region Initialization
        public static void Initialize(ContentManager a_content, PlayerModel a_gamePlayer)
        {
            m_content = a_content;
            m_player = a_gamePlayer;
        }
        #endregion

        #region Public Methods
        public static void LoadLevel(int a_levelNumber)
        {
            TileMap.LoadMap((System.IO.FileStream)TitleContainer.OpenStream(
                @"Content\maps\" + a_levelNumber.ToString().PadLeft(3, '0') + ".map"));

            for (int x = 0; x < TileMap.MapWidth; x++)
            {
                for (int y = 0; y < TileMap.MapHeight; y++)
                {                    
                    if (TileMap.CellTypeValue(x, y) == "START")
                    {
                        //m_player.WorldPos = new Vector2(100, 0);
                        m_player.WorldPos = new Vector2(
                            x * TileMap.TileWidth,
                            (y * TileMap.TileHeight));
                    }
                }
            }

            m_currentLevel = a_levelNumber;
            m_respawnPos = m_player.WorldPos;
        }

        public static void ReloadLevel()
        {
            Vector2 saveRespawn = m_respawnPos;
            LoadLevel(m_currentLevel);
            m_player.WorldPos = m_respawnPos;
        }
        #endregion
    }
}
